package com.tnxst.phexamination.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.activity.IndexActivity;
import com.tnxst.phexamination.activity.LoginActivity;
import com.tnxst.phexamination.constant.Constant;
import com.tnxst.phexamination.databinding.MyFragmentBinding;
import com.tnxst.phexamination.dto.DoctorDTO;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.tnxst.phexamination.vo.UserInfoVo;

import org.apache.commons.collections.CollectionUtils;

public class MyFragment extends Fragment {

    private MyFragmentBinding binding;

    private GlobalViewModel viewModel;

    private DoctorDTO userInfoVo;

    private String account;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        binding = DataBindingUtil.inflate(inflater, R.layout.my_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(requireActivity()).get(GlobalViewModel.class);
        initData();
        initView();
    }

    private void initView() {
        if (StringUtils.isNotNull(userInfoVo.getDoctorName())){
            binding.textView8.setText(userInfoVo.getDoctorName());
        }else {
            binding.textView8.setText("未知");
        }

        if (StringUtils.isNotNull(userInfoVo.getDoctorPhone())){
            binding.textView.setText(userInfoVo.getDoctorPhone());
        }else {
            binding.textView.setText("未知");
        }
        binding.textView3.setText("暂无团队");
//        if (CollectionUtils.isEmpty(userInfoVo.getTeamVos())){
//            binding.textView3.setText("暂无团队");
//        }else {
//            binding.textView3.setText(userInfoVo.getTeamVos().get(0).getTEAMNAME());
//        }

       if (StringUtils.isNull(userInfoVo.getDoctorIdentity()))
           binding.textView4.setText("未知");
       else
           binding.textView4.setText(userInfoVo.getDoctorIdentity());

        if (StringUtils.isNotNull(userInfoVo.getOrganizationName())){
            binding.textView5.setText(userInfoVo.getOrganizationName());
        }else {
            binding.textView5.setText("未知");
        }

        if (StringUtils.isNotNull(userInfoVo.getLoginCode())){
            binding.textView208.setText(userInfoVo.getLoginCode());
        }else {
            binding.textView208.setText("未知");
        }

        binding.textView12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();

            }
        });


    }

    private void initData() {
        userInfoVo = viewModel.getUserInfo();
        account = viewModel.getSharedPreferences().getString(Constant.ACCOUNT,"");
    }

    @Nullable
    @Override
    public View getView() {
        return binding.getRoot();
    }



}
